<template>
  <div id="select-enterprise">
    <div class="items">
      <div class="item" :class="active === index ? 'avtive' : ''" v-for="(item, index) in enterpriseList" @click="selected(item, index)" :key="index">
        <div class="header">
          <div class="title">{{ item.subName }}</div>
          <el-tooltip v-if="item.lastLoginTime" class="item" effect="dark" :content="item.lastLoginTimeVal" placement="right-start">
            <div class="time">上次登录: {{ item.lastLoginTime }}</div>
          </el-tooltip>
        </div>
        <div class="user">
          <div class="avatar" :style="{ background: item.profile ? '' : '#57b2f2' }">
            <img v-if="item.profile" class="avatar-img" :src="item.profile" alt="" />
            <div v-else class="sub-name">{{ item.realName && item.realName.slice(-2) }}</div>
          </div>
          <div class="info">
            <div class="name">{{ item.realName }}</div>
            <div class="post">{{ item.position }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="valid">
      <el-button class="valid-btn" :type="active != null ? 'primary' : 'info'" :disabled="active != null ? false : true" :loading="loading" @click="login">{{ active != null ? "进入企业" : "请选择企业" }}</el-button>
    </div>
  </div>
</template>

<script>
import { acLoginSite } from "@/config/config";
export default {
  name: "selectEnterprise",
  props: {
    enterpriseList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      active: null,
      enterpriseInfo: {},
      loading: false,
    };
  },
  methods: {
    selected(item, index) {
      this.active = index;
      this.enterpriseInfo = item;
    },
    login() {
      const { companyId, userId, singleVal = "", serviceDesc, companynumber, versionTypeDesc } = this.enterpriseInfo;
      if (serviceDesc == "当前企业服务已到期") {
        this.$emit("expire", {
          isExpire: true,
          companynumber: companynumber,
          versionTypeDesc: versionTypeDesc,
        });
        return;
      }
      const params = { companyId, userId, singleVal, ...acLoginSite, username: userId };
      this.loading = true;
      this.$api.publicApi
        .LoginNonTrace(params)
        .then((res) => {
          if (res.data.status == 200) {
            this.$emit("selectLoginSuccess", res.data);
          } else {
            this.$message({ message: res.data.message, type: "warning" });
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="less" scoped>
#select-enterprise {
  .items {
    height: 273px;
    overflow: auto;
    cursor: pointer;
    // padding: 5px 0;
    .item {
      padding: 13px 0;
      text-align: left;
      border-bottom: 1px solid #e4e7ed;
      .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-right: 10px;
        .time {
          font-size: 12px;
          color: #333;
          padding: 0;
          line-height: -1;
        }
      }
      .title {
        color: #555555;
        font-size: 14px;
        width: 200px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .user {
        margin-top: 5px;
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          .avatar-img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
          }
          .sub-name {
            line-height: 40px;
            text-align: center;
            color: #fff;
            font-size: 14px;
          }
        }
        .info {
          margin-left: 10px;
          .name {
            font-size: 14px;
            color: #555555;
          }
          .post {
            font-size: 14px;
            color: #555555;
          }
        }
      }
    }
    .avtive {
      background-color: #99999910;
    }
  }
  .valid {
    margin-top: 23px;
    .valid-btn {
      width: 200px;
      height: 30px;
      font-size: 13px !important;
      line-height: 12px;
      border-radius: 2px;
      font-weight: 600;
      transition: none;
    }
  }
}
</style>
